/**
 * jims
 */
package com.jims.master.modules.doctor.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.dao.ClinicMasterDao;
import com.jims.master.modules.doctor.dao.HisDiagnosisDao;
import com.jims.master.modules.doctor.dao.LiveRequisitionDao;
import com.jims.master.modules.doctor.dao.PatMasterIndexDao;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.LiveRequisition;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.sys.dao.CompanyDao;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 住院信息Service
 * @author chentao
 * @version 2017-03-10
 */
@Service
@Transactional(readOnly = true)
public class LiveRequisitionService extends CrudService<LiveRequisitionDao, LiveRequisition> {

	@Autowired
	private LiveRequisitionDao liveRequisitionDao;
	@Autowired
	private PatMasterIndexDao patMasterIndexDao;
	@Autowired
	private ClinicMasterDao clinicMasterDao;
	@Autowired
	private ClinicMasterService clinicMasterService;
	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private HisDiagnosisDao hisDiagnosisDao;
	@Autowired
	private OrgDeptDao orgDeptDao;

	public LiveRequisition get(String id) {
		return super.get(id);
	}
	
	public List<LiveRequisition> findList(LiveRequisition liveRequisition) {
		return super.findList(liveRequisition);
	}
	
	public Page<LiveRequisition> findPage(Page<LiveRequisition> page, LiveRequisition liveRequisition) {
		return super.findPage(page, liveRequisition);
	}
	
	@Transactional(readOnly = false)
	public int save(LiveRequisition liveRequisition) {
		User user= UserUtils.getUser();
//		liveRequisition.setHospitalizedDept(liveRequisition.getInDept().getId());
		int num = 0;
		if(liveRequisition.getPatMasterIndex()!=null&&StringUtils.isNotBlank(liveRequisition.getPatMasterIndex().getId())){
			PatMasterIndex patMasterIndex = patMasterIndexDao.get(liveRequisition.getPatMasterIndex().getId());
			patMasterIndex.setName(liveRequisition.getName());
			patMasterIndex.setAge(liveRequisition.getAge());
			patMasterIndex.setSex(liveRequisition.getSex());
			patMasterIndex.setBirthdayDate(liveRequisition.getBirthdayDate());
			patMasterIndex.setIdNo(liveRequisition.getIdNo());
			patMasterIndex.setIdentity(liveRequisition.getOccupation());
			patMasterIndex.setUnitInContract(liveRequisition.getUnitInContract());
			patMasterIndex.setNation(liveRequisition.getNation());
			patMasterIndex.setMarriage(liveRequisition.getMarriage());
			patMasterIndex.setHomeplace(liveRequisition.getHomeplace());
			patMasterIndex.setAddress(liveRequisition.getAddress());
			patMasterIndex.setPhone(liveRequisition.getPhone());
			patMasterIndex.setId(liveRequisition.getPatMasterIndex().getId());
			patMasterIndex.preUpdate();
			patMasterIndexDao.update(patMasterIndex);

		}
		ClinicMaster clinicMaster =clinicMasterService.get(liveRequisition.getClinicId());
		String doctorId= clinicMaster.getDoctorId();
		clinicMaster.setClinicType("1");
		if(doctorId == null){
			clinicMaster.setDoctorId(user.getId());
		}
		else{
			clinicMaster.setDoctorId(clinicMaster.getDoctorId());
		}
		clinicMaster.setId(liveRequisition.getClinicId());
		//修改clinic_master挂号状态
		int i = clinicMasterService.updateClinicType(clinicMaster);
		num = super.save(liveRequisition);
		return num;
	}
	
	@Transactional(readOnly = false)
	public int delete(LiveRequisition liveRequisition) {
		return super.delete(liveRequisition);
	}

	/**
	 * 根据ClinicId查询住院通知表信息
	 * @param
	 * @return
	 * @author chentao
	 */
	public LiveRequisition getByClinicId(String clinicId){
		return liveRequisitionDao.getByClinicId(clinicId);
	}

	/**
	 * 修改住院通知单
	 * @param clinicId
	 * @return
	 * @author chentao
	 */
	public int updateRequisition(String clinicId,LiveRequisition liveRequisition){
		return liveRequisitionDao.updateRequisition(clinicId,liveRequisition);
	}

	/**
	 * 修改入院标识
	 * @param clinicId
	 * @author chentao
	 * @return
	 */
	public int upDateInFlge(String clinicId,String inFlag){
		return liveRequisitionDao.upDateInFlge(clinicId,inFlag);
	}

	/**
	 * 
	 * @param liveRequisition
	 * @return
	 */
	public String printLive(LiveRequisition liveRequisition,User user) {
		Map map = new HashMap();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		String hospital = companyDao.get(user.getOrgId()).getOrgName();
		LiveRequisition live = liveRequisitionDao.printInfo(liveRequisition.getClinicId(),user.getOrgId());
		map.put("hospital",hospital);
		map.put("name",live.getName());
		map.put("sex",live.getSex());
		map.put("age",live.getAge());
		map.put("chargeType",live.getChargeType());
		map.put("clinicNo",live.getClinicNo());
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
		map.put("birthdayDate",sdf2.format(live.getBirthdayDate()));
		map.put("occupation",live.getOccupation());
		map.put("marriage",live.getMarriage());
		map.put("nation",live.getNation());
		map.put("idNo",live.getIdNo());
		map.put("homeplace",live.getHomeplace());
		map.put("unitInContract",live.getUnitInContract());
		map.put("contacts",live.getContacts());
		map.put("contactsRelation",live.getContactsRelation());
		map.put("unitInContract",live.getUnitInContract());
		map.put("contactsAddress",live.getContactsAddress());
		map.put("hospitalizedDate",sdf.format(live.getHospitalizedDate()));
		map.put("paymentAdvice",live.getPaymentAdvice());
		map.put("theIllness",live.getTheIllness());
		map.put("diagnosisDesc",live.getDiagnosis());
		map.put("hospitalizedDept",orgDeptDao.get(live.getHospitalizedDept()).getDeptName());
		map.put("quarantine",live.getQuarantine());
		map.put("userName",user.getName());
		map.put("address",live.getAddress());
		map.put("contactsPhone",live.getContactsPhone());
		map.put("phone",live.getPhone());
		map.put("attention",liveRequisitionDao.getByClinicId(liveRequisition.getClinicId()).getAttention());
		map.put("diagnosisDesc",liveRequisitionDao.getByClinicId(liveRequisition.getClinicId()).getDiagnosisDesc());
		LiveRequisition liveRequisition1=liveRequisitionDao.getByClinicId(liveRequisition.getClinicId());
		String printPath = PrintPdfUtils.previewPdf(user.getId(), "his/patHospitalNotice.jasper", map, null, "clinicExam");
		return printPath;
	}
}